<?php
require_once 'FModel/Framework/PropelPDO.php';

abstract class ConnectionFactory {
	protected $connection = null;
	protected $config = array();
	
	protected function __construct() {
		$this->loadDbConfig();
	}
	
	public static function getInstance() {
		return null;
	}
	
	public function __clone() {
		throw new Exception('deny Clone.');
	}
	
	public function getConnection() {
		$dns = isset($this->config['unix_socket'])
			? sprintf('mysql:host=%s;dbname=%s;unix_socket=%s', $this->config['dbhost'], $this->config['dbname'], $this->config['unix_socket'])
			: sprintf('mysql:host=%s;dbname=%s', $this->config['dbhost'], $this->config['dbname']);
		
		if (!$this->connection) {
			
			$this->connection = new PropelPDO($dns, $this->config['dbuser'], $this->config['dbpass'], array(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY => true));
			
			$this->connection->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
			
			//$this->connection->setAttribute(PDO::MYSQL_ATTR_USE_BUFFERED_QUERY,FALSE);
			$this->connection->exec('set names utf8');
		}
		return $this->connection;
	}
	
	public function getSchema() {
		return $this->config['dbname'];
	}
	
	public function closeConnection() {
		$this->connection = null;
	}
	
	abstract protected function loadDbConfig();
}


